package com.yangxd.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yangxd.response.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: yangxudong
 * @Date: 2021/4/20 20:04
 * @Version 1.0
 */
@Component("myAuthenticationSuccessHandler")
public class MyAuthenticationSuccessHandler extends JSONAuthentication implements AuthenticationSuccessHandler {
    /**
     * 登录成功！
     * @param httpServletRequest
     * @param httpServletResponse
     * @param authentication
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        //只需要以JSON的格式返回一个提示
        Result result = Result.ok().message("登录成功");
        //处理编码方式，防止中文乱码的情况
       // httpServletResponse.setContentType("text/json;charset=utf-8");
        //塞到HttpServletResponse中返回给前台
        //这是springboot中jackson给我们提供的一种能将类或者其他对象转成json的方式
       // httpServletResponse.getWriter().write(new ObjectMapper().writeValueAsString(result));
        this.WriteJSON(httpServletRequest,httpServletResponse,result);
    }
}
